package net.hserver.admin.system.filter;

import cn.hserver.core.ioc.annotation.Bean;
import cn.hserver.core.ioc.annotation.Component;
import cn.hserver.core.ioc.annotation.Order;
import cn.hserver.mvc.constants.HttpMethod;
import cn.hserver.mvc.context.WebContext;
import cn.hserver.mvc.filter.FilterAdapter;

@Component
public class CorsFilter implements FilterAdapter {

    @Override
    public void doFilter(WebContext webContext) throws Exception {
        webContext.response.addHeader("Access-Control-Allow-Origin", "*");
        webContext.response.addHeader("Access-Control-Allow-Methods", "*");
        webContext.response.addHeader("Access-Control-Allow-Credentials", "*");
        webContext.response.addHeader("Access-Control-Allow-Headers", "*");
        if (webContext.request.getRequestMethod()== HttpMethod.OPTIONS) {
            webContext.response.sendHtml("");
        }
    }
}